package com.example.myapplication

import android.animation.ObjectAnimator
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var editText: EditText
    private var start = 0F
    private var end = 0F
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val seekBar: MySeekBar = findViewById(R.id.seekbar)
        val reset: Button = findViewById(R.id.reset)
        val to: Button = findViewById(R.id.to)
        editText = findViewById(R.id.edittext)
        reset.setOnClickListener {
            start = 0F
            end = getEnd()
            val animator = ObjectAnimator.ofFloat(seekBar, "progress", start, end)
            animator.start()
        }

        to.setOnClickListener {
            start = end
            end = getEnd()
            val animator = ObjectAnimator.ofFloat(seekBar, "progress", start, end)
            animator.start()
        }
    }

    private fun getEnd(): Float {
        var end = 0F
        val text = editText.text.toString()
        if (text.isNotEmpty()) {
            end = text.toFloat()
            if (end > 100) {
                end = 100F
            }
            if (end < 0) {
                end = 0F
            }
        }
        return end
    }
}